import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;


class Solution1 {
    public int[][] floodFill(int[][] image, int sr, int sc, int color) {
        bfs(image, sr, sc, color, image[sr][sc]);
        return image;
    }

    private void bfs(int[][] image, int sr, int sc, int color, int k) {
        int[] x = {-1, 0, 1, 0};
        int[] y = {0, 1, 0, -1};
        Queue<Integer> qx = new LinkedList<>();
        Queue<Integer> qy = new LinkedList<>();
        qx.add(sr);
        qy.add(sc);
        while (!qx.isEmpty()) {
            int a = qx.poll();
            int b = qy.poll();
            System.out.println(a + " " + b);
            for (int i = 0; i < 4; i++) {
                int xx = a + x[i];
                int yy = b + y[i];
                if (xx >= 0 && xx < image.length && yy >= 0 && yy < image[0].length && image[xx][yy] == k) {
                    qx.add(xx);
                    qy.add(yy);
                }
            }
            image[a][b] = color;
        }
    }
}

class Solution2 {
    public int[][] floodFill(int[][] image, int sr, int sc, int color) {
        int[] x = {-1, 0, 1, 0};
        int[] y = {0, 1, 0, -1};
        Queue<Integer> qx = new LinkedList<>();
        Queue<Integer> qy = new LinkedList<>();
        int k = image[sr][sc];
        qx.add(sr);
        qy.add(sc);
        while (!qx.isEmpty()) {
            int a = qx.poll();
            int b = qy.poll();
            image[a][b] = color;
            System.out.println(a + " " + b);
            for (int i = 0; i < 4; i++) {
                int xx = a + x[i];
                int yy = b + y[i];
                if (xx >= 0 && xx < image.length && yy >= 0 && yy < image[0].length && image[xx][yy] == k) {
                    qx.add(xx);
                    qy.add(yy);
                }
            }
        }
        return image;
    }
}

class Solution3 {
    public int numIslands(char[][] grid) {
        int n = grid.length, m = grid[0].length;
        int count = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (grid[i][j] == '1') {
                    bfs(grid, i, j);
                    System.out.println(i+" "+j);
                    count++;
                }
            }
        }
        return count;
    }

    private void bfs(char[][] grid, int i, int j) {
        Queue<Integer> qx = new LinkedList<>();
        Queue<Integer> qy = new LinkedList<>();
        int n = grid.length, m = grid[0].length;
        qx.add(i);
        qy.add(j);
        grid[i][j] = '2';
        int[] xx = {1, 0, -1, 0};
        int[] yy = {0, 1, 0, -1};
        while (!qx.isEmpty()) {
            int x = qx.poll(), y = qy.poll();
            for (int k = 0; k < 4; k++) {
                int a = x + xx[k];
                int b = y + yy[k];
                if (a >= 0 && a < n && b >= 0 && b < m && grid[a][b] == '1') {
                    qx.add(a);
                    qy.add(b);
                    grid[a][b] = '2';
                }
            }
        }
    }
}

class Solution {
    public int maxAreaOfIsland(int[][] grid) {
        int n = grid.length, m = grid[0].length;
        int ret = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (grid[i][j] == 1) {
                    ret = Math.max(bfs(grid, i, j),ret);
                    System.out.println(i+" "+j);
                }
            }
        }
        return ret;
    }
    private int bfs(int[][] grid, int i, int j) {
        Queue<Integer> qx = new LinkedList<>();
        Queue<Integer> qy = new LinkedList<>();
        int n = grid.length, m = grid[0].length;
        qx.add(i);
        qy.add(j);
        grid[i][j] = 2;
        int[] xx = {1, 0, -1, 0};
        int[] yy = {0, 1, 0, -1};
        int count = 1;
        while (!qx.isEmpty()) {
            int x = qx.poll(), y = qy.poll();
            for (int k = 0; k < 4; k++) {
                int a = x + xx[k];
                int b = y + yy[k];
                if (a >= 0 && a < n && b >= 0 && b < m && grid[a][b] == 1) {
                    qx.add(a);
                    qy.add(b);
                    grid[a][b] = 2;
                    count++;
                }
            }
        }
        return count;
    }
}

class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int[][] map = new int[1000][1000];
        for(int i = 0; i<1000; i+=4) {
            for(int j = 0; j<1000; j+=4) {
                map[i][j] = 1;
                map[i+1][j] = 1;
                map[i][j+1] = 1;
                map[i+1][j+1] = 1;
            }
        }
        for(int i = 2; i<1000; i+=4) {
            for(int j = 2; j<1000; j+=4) {
                map[i][j] = 1;
                map[i+1][j] = 1;
                map[i][j+1] = 1;
                map[i+1][j+1] = 1;
            }
        }

        int count = 0;
        for(int i = 0; i<n; i++) {
            for(int j = 0; j<m; j++) {
                count += map[i][j];
            }
        }

        System.out.println(count);
    }

    public static int StrToInt(String str) {
        if(str.length() == 0) return 0;
        int i = 0;
        if(str.charAt(0) == '+' || str.charAt(0) == '-') {
            i++;
        }
        int ret = 0;
        for( ; i<str.length(); i++) {
            char ch = str.charAt(i);
            if(ch<='0' || ch>='9') {
                return 0;
            }
            ret = ret*10+(ch-'0');
        }
        if(str.charAt(0) == '-') return -ret;
        return ret;
    }

    public static void main3(String[] args) {
        int ret = StrToInt("1a33");
        System.out.println(ret);
    }



    public static void main2(String[] args) {
        char[][] grid = {{'1','1','1','1','0'},
                         {'1','1','0','1','0'},
                         {'1','1','0','0','0'},
                         {'0','0','0','0','0'}};
        int ret = new Solution3().numIslands(grid);
        System.out.println(ret);
    }
    public static void main1(String[] args) {
        int[][] nums = {{1, 1, 1}, {1, 1, 0}, {1, 0, 1}};
        nums = new Solution2().floodFill(nums, 1, 1, 2);
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.print(nums[i][j] + " ");
            }
            System.out.println();
        }
    }
}
